package com.xqboss.apps.service.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.user.UserRankUpgradeRecord;
import com.xqboss.apps.dto.user.UserRankUpgradeRecordDto;
import com.xqboss.apps.enums.user.UserIdentityPromotionEnum;
import com.xqboss.apps.mapper.user.UserRankUpgradeRecordMapper;
import com.xqboss.apps.vo.user.IdentityRankUpgradeVo;
import com.xqboss.apps.vo.user.UserRankUpgradeRecordVo;
import com.xqboss.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户段位升级记录(UserRankUpgradeRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-12-05 12:05:24
 */
@Service
public class UserRankUpgradeRecordService extends ServiceImpl<UserRankUpgradeRecordMapper, UserRankUpgradeRecord>  {

    public List<UserRankUpgradeRecordVo> rankUpgradeRecordVo(List<UserRankUpgradeRecord> userRankUpgradeRecords) {
        // 组装数据
        ArrayList<UserRankUpgradeRecordVo> voList = new ArrayList<>();
        UserRankUpgradeRecordVo vo;
        for (UserRankUpgradeRecord e : userRankUpgradeRecords) {
            vo = new UserRankUpgradeRecordVo();
            BeanUtils.copyProperties(e,vo);
            voList.add(vo);
        }
        return voList;
    }

    public List<UserRankUpgradeRecord> rankUpgradeRecord(UserRankUpgradeRecordDto dto) {
        // 执行查询
        List<UserRankUpgradeRecord> userRankUpgradeRecords = this.lambdaQuery()
                .like(StringUtils.isNotBlank(dto.getPreRankFullName()),UserRankUpgradeRecord::getPreRankFullName, dto.getPreRankFullName())
                .like(StringUtils.isNotBlank(dto.getPostRankFullName()),UserRankUpgradeRecord::getPostRankFullName, dto.getPostRankFullName())
                .like(StringUtils.isNotBlank(dto.getResourceCost()),UserRankUpgradeRecord::getResourceCost, dto.getResourceCost())
                .eq(dto.getUserId()!=null,UserRankUpgradeRecord::getUserId, dto.getUserId())
                .ge(dto.getCreateTimeStart()!=null,UserRankUpgradeRecord::getCreateTime, dto.getCreateTimeStart())
                .le(dto.getCreateTimeEnd()!=null,UserRankUpgradeRecord::getCreateTime, dto.getCreateTimeEnd())
                .orderByDesc(UserRankUpgradeRecord::getCreateTime)
                .list();
        return userRankUpgradeRecords;
    }

    /**
     * 新增用户段位升级记录
     *
     * @param userId 用户id
     * @param vo 请求对象
     */
    @Transactional(rollbackFor=Exception.class)
    public void insertUserRankUpgradeRecord(Long userId,IdentityRankUpgradeVo vo) {
        //设置资源消耗
        vo.setResourceCost(UserIdentityPromotionEnum.ResourceCost(vo.getSpendNftType()));
        UserRankUpgradeRecord record = UserRankUpgradeRecord.builder()
                .userId(userId)
                .nftNumberId(vo.getNftNumberId())
                .preRankFullName(vo.getCurrentIdentity())
                .postRankFullName(vo.getPromotionIdentity())
                .preRankId(vo.getCurrentIdentityId())
                .postRankId(vo.getPromotionIdentityId())
                .resourceCost(vo.getResourceCost())
                .upgradeTime(new Date())
                .build();
        this.save(record);
    }
}

